package com.example.wisdomclassroomapi.domain.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.wisdomclassroomapi.domain.entity.*;
import com.example.wisdomclassroomapi.infrastructure.mapper.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Package: com.example.wisdomclassroomapi.domain.repository
 * @Description: <>
 * @Author: scholes
 * @CreateDate: 2022-04-18
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-18
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Repository
public class AuthUserRepository {
    @Resource
    private AuthUserMapper authUserMapper;
    @Resource
    private AuthUrBindMapper authUrBindMapper;
    @Resource
    private AuthRoleMapper authRoleMapper;
    @Resource
    private AuthRpBindMapper authRpBindMapper;
    @Resource
    private AuthPermissionMapper authPermissionMapper;


    public void add(AuthUserEntity entity) {
       authUserMapper.insert(entity);
    }

    public AuthUserEntity findById(String id) {
        return authUserMapper.selectById(id);
    }

    public int updateById(AuthUserEntity entity) {
        return authUserMapper.updateById(entity);
    }

    public int deleteById(String id) {
        return authUserMapper.deleteById(id);
    }

    public IPage<AuthUserEntity> page(AuthUserEntity entity,IPage page) {
        QueryWrapper<AuthUserEntity> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(entity.getUsername())){
            wrapper.eq("username",entity.getUsername());
            }
            if (StringUtils.isNotBlank(entity.getEmail())){
            wrapper.eq("email",entity.getEmail());
            }
            if (StringUtils.isNotBlank(entity.getMobile())){
            wrapper.eq("mobile",entity.getMobile());
            }
            if (StringUtils.isNotBlank(entity.getPassword())){
            wrapper.eq("password",entity.getPassword());
            }
            if (StringUtils.isNotBlank(entity.getRealName())){
            wrapper.eq("real_name",entity.getRealName());
            }
            if (StringUtils.isNotBlank(entity.getType())){
            wrapper.eq("type",entity.getType());
            }
            if (StringUtils.isNotBlank(entity.getStatus())){
            wrapper.eq("status",entity.getStatus());
            }
            if (StringUtils.isNotBlank(entity.getStudentNo())){
            wrapper.eq("student_no",entity.getStudentNo());
            }
        return authUserMapper.selectPage(page, wrapper);
    }

    public List<AuthUserEntity> list(AuthUserEntity entity) {
        QueryWrapper<AuthUserEntity> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(entity.getUsername())){
            wrapper.eq("username",entity.getUsername());
            }
            if (StringUtils.isNotBlank(entity.getEmail())){
            wrapper.eq("email",entity.getEmail());
            }
            if (StringUtils.isNotBlank(entity.getMobile())){
            wrapper.eq("mobile",entity.getMobile());
            }
            if (StringUtils.isNotBlank(entity.getPassword())){
            wrapper.eq("password",entity.getPassword());
            }
            if (StringUtils.isNotBlank(entity.getRealName())){
            wrapper.eq("real_name",entity.getRealName());
            }
            if (StringUtils.isNotBlank(entity.getType())){
            wrapper.eq("type",entity.getType());
            }
            if (StringUtils.isNotBlank(entity.getStatus())){
            wrapper.eq("status",entity.getStatus());
            }
            if (StringUtils.isNotBlank(entity.getStudentNo())){
            wrapper.eq("student_no",entity.getStudentNo());
            }
        return authUserMapper.selectList(wrapper);
    }


    public AuthUserEntity findByUsername(String username) {
        QueryWrapper<AuthUserEntity> wrapper = new QueryWrapper<AuthUserEntity>()
                .eq("username", username);
        return authUserMapper.selectOne(wrapper);
    }


    public Set<SimpleGrantedAuthority> getAuthCodeByUserId(String userId) {
        Set<SimpleGrantedAuthority> result = new HashSet<>();

        QueryWrapper<AuthUrBindEntity> wrapper1 = new QueryWrapper<AuthUrBindEntity>()
                .eq("user_id", userId);
        List<AuthUrBindEntity> list = authUrBindMapper.selectList(wrapper1);
        list.parallelStream().forEach(bindPo -> {
            QueryWrapper<AuthRpBindEntity> wrapper2 = new QueryWrapper<AuthRpBindEntity>()
                    .eq("role_id", bindPo.getRoleId());
            List<AuthRpBindEntity> authRpBindEntities = authRpBindMapper.selectList(wrapper2);

            AuthRoleEntity authRoleEntity = authRoleMapper.selectById(bindPo.getRoleId());
            result.add(new SimpleGrantedAuthority("ROLE_"+authRoleEntity.getCode()));

            authRpBindEntities.parallelStream().forEach(bindPo2 -> {
                AuthPermissionEntity authPermissionEntity = authPermissionMapper.selectById(bindPo2.getPermissionId());
                result.add(new SimpleGrantedAuthority(authPermissionEntity.getCode()));
            });
        });

        return result;
    }

}